package gov.nhso.prov.acc.servlets;

import gov.nhso.prov.acc.core.ConfigBundle;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class ServletFilter implements Filter {
	private FilterConfig config;
	public void init(FilterConfig filterConfig) throws ServletException {
		this.config = filterConfig;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req =(HttpServletRequest)request;
		ServletContext context = this.config.getServletContext();
	    if (req.getSession().getAttribute("userInfo") == null) {
	    	req.setAttribute("version", ConfigBundle.getValue("VERSION"));
	    	req.setAttribute("deployDate", ConfigBundle.getValue("DEPLOY_DATE"));
	    	req.setAttribute("deployTime", ConfigBundle.getValue("DEPLOY_TIME"));
	    	context.getRequestDispatcher("/index.jsp").forward(request, response);
	    } else {
	        chain.doFilter(request, response);
	    }
	}

	public void destroy() {

	}
}